﻿namespace NTLib.WPF.Interactions
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Collections.Specialized;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Markup;

    /// <summary>
    /// Main class interaction that permit to connect the interaction behavior to Xaml element.
    /// </summary>
    public sealed class Interaction : BaseInteractionContainer<BaseTrigger>
    {
        #region Properties

        #region Attached Property

        /// <summary>
        /// Gets the attach.
        /// </summary>
        /// <param name="obj">The object.</param>
        /// <returns></returns>
        public static Interaction GetAttach(DependencyObject obj)
        {
            return (Interaction)obj.GetValue(AttachProperty);
        }

        /// <summary>
        /// Sets the attach.
        /// </summary>
        /// <param name="obj">The object.</param>
        /// <param name="value">The value.</param>
        public static void SetAttach(DependencyObject obj, Interaction value)
        {
            obj.SetValue(AttachProperty, value);
            
            if (value != null)
            {
                if (value.AttachedObject != null)
                    value.Detached();
                value.Attached(obj);
            }
        }

        // Using a DependencyProperty as the backing store for Interaction.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty AttachProperty =
            DependencyProperty.RegisterAttached("Attach", typeof(Interaction), typeof(DependencyObject), new PropertyMetadata(null, InteractionAttachChanged));

        #endregion

        #endregion

        #region Methods

        /// <summary>
        /// Interactions the attach changed.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="args">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
        private static void InteractionAttachChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            if (args.OldValue != null)
                (args.OldValue as Interaction).Detached();

            if (args.NewValue != null)
                (args.NewValue as Interaction).Attached(sender);
        }

        #endregion
    }
}
